﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation;

namespace WorkItemVisualization
{
    /// <summary>
    /// Implements a service provider that allows opening of a work item when the user double clicks in a work
    /// item in a list (eg: related work items list)
    /// </summary>
    internal class WorkItemEditorServiceProvider : IServiceProvider
    {
        private WorkItemWindow _workItemWindow;

        #region inner class
        internal class WorkItemEditorClientLinking : IClientLinking
        {
            WorkItemWindow _workItemEditor;

            internal WorkItemEditorClientLinking(WorkItemWindow workItemEditor)
            {
                this._workItemEditor = workItemEditor;
            }

            public bool ExecuteDefaultAction(string artifactUri, string contextInfo)
            {
                ArtifactId artifactURI;

                artifactURI = LinkingUtilities.DecodeUri(artifactUri);

                _workItemEditor.ShowWorkItem(Convert.ToInt32(artifactURI.ToolSpecificId));

                return true;
            }
        }
        #endregion

        public WorkItemEditorServiceProvider(WorkItemWindow workItemWindow)
        {
            this._workItemWindow = workItemWindow;
        }

        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IClientLinking))
            {
                return new WorkItemEditorClientLinking(_workItemWindow);
            }

            return null;
        }
    }
}
